package com.sun.java8;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestStream {
    @Test
    public void test01() {

    }

    @Test
    public void test02() {
        //通过collection系列集合提供的stream()或parallelStream() 获得流
        List<String> list = new ArrayList<>();
        Stream<String> stream = list.stream();
        //通过Arrays的静态方法stream()获取
        Person[] persons = new Person[10];
        Stream<Person> stream1 = Arrays.stream(persons);
        //通过Stream类中的静态方法of()
        Stream<String> stream2 = Stream.of("aa", "BB", "CC");
        stream2.forEach((x) -> System.out.println(x));
        //创建无限流
        Stream<Integer> stream3 = Stream.iterate(0, (x) -> x + 2);
        stream3.limit(10).forEach(System.out::println);
    }

    /*
    筛选与切片
    filter-接收lambda,从流中排除一些元素
    limit--截断流,使其元素不超过给定数量
    skip(n)--跳过元素，返回一个扔掉了前n个元素的流，若元素中不足n，则返回一个空流，与limit(n)互补
    distinct--筛选，通过流所生成元素的hashCode()和equals()去除重复元素
     */
    @Test
    public void test03() {
        List<Person> peoples = Arrays.asList(
                new Person(1, "张四", 18),
                new Person(1, "张三", 18),
                new Person(3, "张五", 19),
                new Person(4, "张六", 21),
                new Person(4, "张六", 25)
        );
        Stream<Person> stream = peoples.stream().filter(x -> x.getAge() > 18).limit(3);
        List<Person> list = stream.collect(Collectors.toList());
//        stream.forEach(System.out::println);
        System.out.println(list);
//        System.out.println(peoples);
    }

    @Test
    public void test04() {
        List<Person> peoples = Arrays.asList(
                new Person(1, "张四", 18),
                new Person(1, "张三", 18),
                new Person(3, "张五", 19),
                new Person(4, "张六", 21),
                new Person(4, "张六", 25)
        );
        Stream<Person> stream = peoples.stream().filter(x -> x.getAge() > 18).skip(1);
        List<Person> list = stream.collect(Collectors.toList());
//        stream.forEach(System.out::println);
        System.out.println(list);
        System.out.println(peoples);
    }

    @Test
    public void test05() {
        List<Person> peoples = Arrays.asList(
                new Person(1, "张四", 18),
                new Person(1, "张三", 18),
                new Person(3, "张五", 19),
                new Person(3, "张五", 19),
                new Person(4, "张六", 21),
                new Person(4, "张六", 25)
        );
        Stream<Person> stream = peoples.stream().filter(x -> x.getAge() > 18).distinct();
        List<Person> list = stream.collect(Collectors.toList());

        System.out.println(list);
    }

    @Test
    public void test5() {
        List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
        list.stream()
                .map(x -> x.toUpperCase())
                .forEach(System.out::println);
        List<Person> peoples = Arrays.asList(
                new Person(1, "张四", 18),
                new Person(1, "张三", 18),
                new Person(3, "张五", 19),
                new Person(3, "张五", 19),
                new Person(3, "张五", 19),
                new Person(4, "张六", 21),
                new Person(4, "张六", 25)
        );
        List<String> collect = peoples.stream().filter(x -> x.getAge() > 18).map(x -> x.getName())
                .collect(Collectors.toList());
        System.out.println(collect);
    }
    @Test
    public void test6() {
        List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
        Stream<Stream<Character>> streamStream = list.stream()
                .map(x -> getCharacter(x));
        streamStream.forEach(x->{
            x.forEach(System.out::println);
        });
        List<Person> peoples = Arrays.asList(
                new Person(1, "张四", 18),
                new Person(1, "张三", 18),
                new Person(3, "张五", 19),
                new Person(3, "张五", 19),
                new Person(3, "张五", 19),
                new Person(4, "张六", 21),
                new Person(4, "张六", 25)
        );
        List<String> collect = peoples.stream().filter(x -> x.getAge() > 18).map(x -> x.getName())
                .collect(Collectors.toList());
        System.out.println(collect);
    }

    @Test
    public void test7() {
        List<String> list = Arrays.asList("aa", "bb", "cc", "dd");
        Stream<Character> streamStream = list.stream()
                .flatMap(x -> getCharacter(x));
        streamStream.forEach(System.out::println);
    }
    public static Stream<Character> getCharacter(String str){
        List<Character> list = new ArrayList<>();
        for (char ch : str.toCharArray()) {
            list.add(ch);
        }
        return list.stream();
    }


    @Test
    public void test8() {
        List<String> list = Arrays.asList("ee","aa", "bb", "cc", "dd");
         list.stream()
                .sorted()
                .forEach(System.out::println);
    }
    @Test
    public void test9() {
        List<Person> peoples = Arrays.asList(
                new Person(1, "张四", 18),
                new Person(1, "张三", 30),
                new Person(3, "张五", 19),
                new Person(3, "张五", 19),
                new Person(4, "张五", 19),
                new Person(4, "张六", 26),
                new Person(4, "张六", 25)
        );
        peoples.stream()
                .sorted((x,y)->{
                    if (x.getId().equals(y.getId())){
                        return x.getAge().compareTo(y.getAge());
                    }else {
                        return x.getId().compareTo(y.getId());
                    }
                }).forEach(System.out::println);
    }
}
